package site.koudai.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import site.koudai.cloud.entity.*;
import site.koudai.cloud.mapper.*;
import site.koudai.cloud.service.UserService;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private DataRuleMapper dataRuleMapper;

    @Override
    public User getUserInfo(Integer id) {
        User user = getById(id);
        List<Role> roleList = roleMapper.getRolesByUserId(id);
        List<Permission> permissionList = permissionMapper.getPermissionsByUserId(id);
        user.setRoleList(roleList);
        user.setPermissionList(permissionList);
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", roleList.stream().map(Role::getId).collect(Collectors.toList()));

        List<RolePermission> rolePermissionList = rolePermissionMapper.selectList(queryWrapper);

        Map<String, List<RolePermission>> rolePermissionMap = rolePermissionList.stream().collect(Collectors.groupingBy(rolePermission -> rolePermission.getRoleId() + ""));
        Map<String, Set<String>> permissionAllowFieldsMap = new HashMap<>();
        Map<String, List<List<String>>> permissionDataRuleMap = new HashMap<>();
        List<String> dataRuleIds = new ArrayList<>();
        rolePermissionMap.keySet().forEach(key -> {
            rolePermissionMap.get(key).forEach(rolePermission -> {
                String permissionId = rolePermission.getPermissionId() + "";
                List<String> tempAllowFields = Arrays.asList(rolePermission.getAllowFields().split(","));
                if (!permissionAllowFieldsMap.containsKey(permissionId)) {
                    Set<String> allowFields = new HashSet<>(tempAllowFields);
                    permissionAllowFieldsMap.put(permissionId, allowFields);
                } else {
                    permissionAllowFieldsMap.get(permissionId).addAll(tempAllowFields);
                }

                String ruleIds = rolePermission.getDataRuleIds();
                if (ruleIds != null && !ruleIds.isEmpty()) {
                    List<String> tempDataRuleIds = Arrays.asList(ruleIds.split(","));
                    dataRuleIds.addAll(tempDataRuleIds);

                    if (!permissionDataRuleMap.containsKey(permissionId)) {
                        List<List<String>> dataRuleList = new ArrayList<>();
                        dataRuleList.add(tempDataRuleIds);
                        permissionDataRuleMap.put(permissionId, dataRuleList);
                    } else {
                        permissionDataRuleMap.get(permissionId).add(tempDataRuleIds);
                    }
                }

            });
        });

        List<DataRule> dataRuleList = dataRuleMapper.selectByIds(dataRuleIds);
        Map<String, DataRule> dataRuleMap = dataRuleList.stream().collect(Collectors.toMap(dataRule -> dataRule.getId() + "", dataRule -> dataRule));

        permissionList.forEach(permission -> {
            String permissionId = permission.getId() + "";

            permission.setAllowFields(permissionAllowFieldsMap.get(permissionId));

            List<List<String>> list = permissionDataRuleMap.get(permissionId);
            List<List<DataRule>> permissionDataRuleList = new ArrayList<>();
            if (list != null) {
                list.forEach(item -> {
                    List<DataRule> tempDataRuleList = new ArrayList<>();
                    item.forEach(dataRuleId -> {
                        if (dataRuleId != null && !dataRuleId.isEmpty()) {
                            tempDataRuleList.add(dataRuleMap.get(dataRuleId));
                        }
                    });
                    if (!tempDataRuleList.isEmpty()) {
                        permissionDataRuleList.add(tempDataRuleList);
                    }
                });
            }
            permission.setDataRuleList(permissionDataRuleList);
        });

        return user;
    }

}
